package BFS.review;

import 其他.DataStructure.TreeNode;
import MyUtils.TreeUtils;
import org.junit.Test;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Q vis +rot
 *
 * w q
 * sz
 * for sz
 *  cur
 *  if
 *  {for岛/if树}
 *  step!
 */
public class 二叉树的最小深度111 {

    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int step = 1;

        while (!q.isEmpty()){
            int oldsz = q.size();
            for (int i = 0; i < oldsz; i++) {
                TreeNode cur = q.poll();

                if (cur.left==null && cur.right==null){
                    return step;
                }

                if (cur.left!=null){
                    q.offer(cur.left);
                }
                if (cur.right!=null){
                    q.offer(cur.right);
                }
            }

            step++; // for oldSize / if 左右 -> step层+1 勿少

        }
        return step;
    }

    /**
     *      1
     *     2 3
     *    4 5
     */
    @Test
    public void test() {
        TreeNode root = TreeUtils.strToTree("1,2,3,4,5");
        System.out.println("RES:" + minDepth(root));
    }
}
